//
//  SISSIntendTenantDetailViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/1/8.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSIntendTenantDetailViewController.h"
#import "SISSTitleView.h"
#import "SISSShowView.h"
#import "SISSNetworkingManager+TenantProgress.h"
#import "SISSNetworkingManager+Progress.h"
#import "SISSTenantIntentionRentOutModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "SISSRentOutDetailViewController.h"

@interface SISSIntendTenantDetailViewController ()

/// topView
@property (weak, nonatomic) UIView *topView;

/* imageView */
@property (weak, nonatomic) UIImageView *imageView;
/* topLabel */
@property (weak, nonatomic) UILabel *topLabel;
/* middleLabel */
@property (weak, nonatomic) UILabel *middleLabel;
/* labelsView */
@property (weak, nonatomic) UIView *labelsView;
@property (strong, nonatomic) NSArray<UILabel *> *markLabels;

/* orderTitleView */
@property (weak, nonatomic) SISSTitleView *orderTitleView;
/* takeLookDateView */
@property (weak, nonatomic) SISSShowView *takeLookDateView;
/* phoneView */
@property (weak, nonatomic) SISSShowView *phoneView;
/* regionView */
@property (weak, nonatomic) SISSShowView *regionView;
/* detailLabel */
@property (weak, nonatomic) UILabel *detailLabel;
@property (weak, nonatomic) UIView *lineView;
/* rentView */
@property (weak, nonatomic) SISSShowView *rentView;
/* orderHouseTitleView */
@property (weak, nonatomic) SISSTitleView *orderHouseTitleView;

/* confirmButton */
@property (weak, nonatomic) UIButton *confirmButton;
/* refuseButton */
@property (weak, nonatomic) UIButton *refuseButton;

/* <#Annotation#> */
@property (strong, nonatomic) SISSTenantIntentionRentOutModel *intendRentOutModel;

@end

@implementation SISSIntendTenantDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"意向出租房源";
}

- (void)setIntendRentOutModel:(SISSTenantIntentionRentOutModel *)intendRentOutModel {
    _intendRentOutModel = intendRentOutModel;
    
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:intendRentOutModel.lease.image.firstObject] placeholderImage:[UIImage imageNamed:@"头像"]];
    self.topLabel.text = [NSString stringWithFormat:@"%@ | %@",intendRentOutModel.lease.neighbourhood, intendRentOutModel.lease.monthlyRent];
    self.middleLabel.text = [NSString stringWithFormat:@"%@-%@ | %@㎡ | %@",intendRentOutModel.lease.region,intendRentOutModel.lease.regionDetail,intendRentOutModel.lease.area,intendRentOutModel.lease.houseType];
    NSArray *houseFeatureArr = intendRentOutModel.lease.houseFeature.length == 0 ? @[] : [intendRentOutModel.lease.houseFeature componentsSeparatedByString:@"#"];
    for (UILabel *label in self.markLabels) {
        label.hidden = YES;
    }
    
    for (NSInteger i = 0; i < houseFeatureArr.count ; i++) {
        UILabel *label = self.markLabels[i];
        label.hidden = NO;
        label.text = houseFeatureArr[i];
        label.layer.borderWidth = 1;
        label.text = [NSString stringWithFormat:@"%@",houseFeatureArr[i]];
        UIColor *color = [SISSPublicMethods matchTextColorWithHouseFeatureStr:houseFeatureArr[i]];
        label.layer.borderColor = color.CGColor;
        label.textColor = color;
        label.layer.cornerRadius = 2;
        label.layer.masksToBounds = YES;
    }
    [self.labelsView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (intendRentOutModel.lease.houseFeature.length == 0) {
            make.height.mas_equalTo(30);
        } else {
            make.height.mas_equalTo(self.markLabels[houseFeatureArr.count - 1].frame.origin.y + 30);
        }
        
    }];
    
    self.takeLookDateView.rightStr = [NSDate yyr_stringToString:intendRentOutModel.takeLookDate];
    self.phoneView.rightStr = intendRentOutModel.takelookMobile;
    self.rentView.rightStr = [NSString stringWithFormat:@"%@-%@",intendRentOutModel.takelookRegion,intendRentOutModel.takelookRegionDetail];
    self.detailLabel.text = intendRentOutModel.takelookOtherdesc;
    self.rentView.rightStr = intendRentOutModel.takelookMinPrice > 0 ? [NSString stringWithFormat:@"%zd-%zd元",intendRentOutModel.takelookMinPrice,intendRentOutModel.takelookMaxPrice] : @"";
    
    if (!self.isHiddenBottom) {
        
        self.confirmButton.hidden = intendRentOutModel.state != 0;
        self.refuseButton.hidden = intendRentOutModel.state != 0;
    }
}

- (void)setIsHiddenBottom:(NSInteger)isHiddenBottom {
    _isHiddenBottom = isHiddenBottom;
    self.orderHouseTitleView.hidden = isHiddenBottom;
    self.regionView.hidden = isHiddenBottom;
    self.detailLabel.hidden = isHiddenBottom;
    self.lineView.hidden = isHiddenBottom;
    self.rentView.hidden = isHiddenBottom;
}

/// 拒绝按钮点击事件
- (void)updateLeaseStateButtonClickAction:(UIButton *)button {
    
    NSInteger state = button.tag;
    
    [kNetworkManager updateLeaseTakeLookAppointmentStateWithId:self.intendRentOutModel.altlaId state:state success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 200) {
            NSString *tipStr = state == 1 ? @"预约成功" : @"已拒绝";
            [SISSTools showSVSuccessTitle:tipStr andTime:2 andColor:YES];
            self.confirmButton.hidden = YES;
            self.refuseButton.hidden = YES;
        }
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)setArbpId:(NSString *)arbpId {
    _arbpId = arbpId;
    
    WeakSelf
    [kNetworkManager requestTenantIntentionRentOutWithArbpId:arbpId success:^(SISSTenantIntentionRentOutModel *intendRentOutModel) {
        
        weakSelf.noDataShowView.hidden = intendRentOutModel != nil;
        weakSelf.scrollView.hidden = !intendRentOutModel;
        weakSelf.intendRentOutModel = intendRentOutModel;
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)tapRentOutInfoAction {
    
    SISSRentOutDetailViewController *vc = [[SISSRentOutDetailViewController alloc] init];
    vc.title = @"出租详情";
    vc.rentOutModel = self.intendRentOutModel.lease;
    vc.isHiddenBottomButton = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    UIView *topView = [[UIView alloc] init];
    topView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.containerView addSubview:topView];
    self.topView = topView;
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRentOutInfoAction)];
    [self.topView addGestureRecognizer:tapGesture];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"头像"];
    [self.topView addSubview:imageView];
    self.imageView = imageView;
    
    UILabel *topLabel = [[UILabel alloc] init];
    topLabel.font = [UIFont systemFontOfSize:16];
    topLabel.textColor = MAIN_TEXT_COLOR;
    topLabel.text = @"小区 | 租金";
    [self.topView addSubview:topLabel];
    self.topLabel = topLabel;
    
    // 详细信息
    UILabel *middleLabel = [[UILabel alloc] init];
    middleLabel.font = [UIFont systemFontOfSize:16];
    middleLabel.textColor = MAIN_TEXT_COLOR;
    middleLabel.text = @"区域 | 面积 | 户型";
    [self.topView addSubview:middleLabel];
    self.middleLabel = middleLabel;
    
    UIView *labelsView = [[UIView alloc] init];
//    labelsView.backgroundColor = [UIColor redColor];
    [self.topView addSubview:labelsView];
    self.labelsView = labelsView;
    
    NSMutableArray<UILabel *> *arrM = [NSMutableArray arrayWithCapacity:6];
    float labelX = 20;
    float labelY = 0;
    CGFloat labelWidth = 66;
    for (NSInteger i = 0; i < 6; i++) {
        if (labelX + labelWidth + 20 > (SCREEN_WIDTH-120)) {
            labelX = 20;
            labelY += 36;
        }
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(labelX, labelY, labelWidth, 28)];
        label.textColor = SECONDARY_TEXT_COLOR;
        label.font = [UIFont systemFontOfSize:14];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"房屋特色";
        label.layer.borderWidth = 1;
        label.layer.cornerRadius = 4;
        label.layer.borderColor = SECONDARY_TEXT_COLOR.CGColor;
        //        label.layer.masksToBounds = YES;
        [arrM addObject:label];
        [self.labelsView addSubview:label];
        
        labelX = CGRectGetMaxX(label.frame) + 10;
        label.hidden = YES;
        
    }
    
    self.markLabels = arrM;
    
    SISSTitleView *orderTitleView = [[SISSTitleView alloc] init];
    orderTitleView.titleStr = @"承租方预约信息";
    [self.containerView addSubview:orderTitleView];
    self.orderTitleView = orderTitleView;
    
    SISSShowView *takeLookDateView = [[SISSShowView alloc] init];
    takeLookDateView.leftStr = @"看房日期：";
    takeLookDateView.isShowBottomLine = YES;
    [self.containerView addSubview:takeLookDateView];
    self.takeLookDateView = takeLookDateView;
    
    SISSShowView *phoneView = [[SISSShowView alloc] init];
    phoneView.leftStr = @"手机号码：";
    phoneView.isShowBottomLine = YES;
    [self.containerView addSubview:phoneView];
    self.phoneView = phoneView;
    
    SISSTitleView *orderHouseTitleView = [[SISSTitleView alloc] init];
    orderHouseTitleView.titleStr = @"意向求租信息";
    [self.containerView addSubview:orderHouseTitleView];
    self.orderHouseTitleView = orderHouseTitleView;
    
    SISSShowView *regionView = [[SISSShowView alloc] init];
    regionView.leftStr = @"意向区域：";
    regionView.isShowBottomLine = YES;
    [self.containerView addSubview:regionView];
    self.regionView = regionView;
    
    UILabel *detailLabel = [[UILabel alloc] init];
    detailLabel.numberOfLines = 0;
    [self.containerView addSubview:detailLabel];
    self.detailLabel = detailLabel;
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.containerView addSubview:lineView];
    self.lineView = lineView;
    
    SISSShowView *rentView = [[SISSShowView alloc] init];
    rentView.leftStr = @"租金预算：";
    rentView.isShowBottomLine = YES;
    [self.containerView addSubview:rentView];
    self.rentView = rentView;
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:@"同意" forState:UIControlStateNormal];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:MAIN_COLOR];
    confirmButton.layer.cornerRadius = 8;
    [confirmButton addTarget:self action:@selector(updateLeaseStateButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    confirmButton.tag = 1;
    [self.containerView addSubview:confirmButton];
    self.confirmButton = confirmButton;
    
    UIButton *refuseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [refuseButton setTitle:@"拒绝" forState:UIControlStateNormal];
    [refuseButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [refuseButton setBackgroundColor:MAIN_COLOR];
    refuseButton.layer.cornerRadius = 8;
    refuseButton.tag = 2;
    [refuseButton addTarget:self action:@selector(updateLeaseStateButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.containerView addSubview:refuseButton];
    self.refuseButton = refuseButton;
    
    self.orderHouseTitleView.hidden = self.isHiddenBottom;
    self.regionView.hidden = self.isHiddenBottom;
    self.detailLabel.hidden = self.isHiddenBottom;
    self.lineView.hidden = self.isHiddenBottom;
    self.rentView.hidden = self.isHiddenBottom;
    self.confirmButton.hidden = self.isHiddenBottom;
    self.refuseButton.hidden = self.isHiddenBottom;
}

#pragma mark - 布局
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat margin = 8.0;
    CGSize imageSize = CGSizeMake(80, 80);
    CGFloat showViewHeight = 44;
    CGSize buttonSize = CGSizeMake((SCREEN_WIDTH - 60) * 0.5, 44);
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.topView).offset(margin);
        make.top.equalTo(weakSelf.topView).offset(2 * margin);
        make.size.mas_equalTo(imageSize);
    }];
    
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.imageView);
        make.left.equalTo(weakSelf.imageView.mas_right).offset(margin);
    }];
    
    [self.middleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.topLabel.mas_bottom).offset(2 * margin);
        make.left.equalTo(weakSelf.topLabel);
    }];
    
    [self.labelsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.topLabel);
        make.top.equalTo(weakSelf.middleLabel.mas_bottom).offset(margin);
        make.right.equalTo(weakSelf.topView).offset(-2 * margin);
        make.height.mas_equalTo(weakSelf.markLabels[2].frame.origin.y + 30);
        make.bottom.equalTo(weakSelf.topView).offset(-margin);
    }];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(weakSelf.containerView).offset(2 * margin);
        make.right.equalTo(weakSelf.containerView).offset(-2 * margin);
    }];
    
    [self.orderTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.containerView);
        make.top.equalTo(weakSelf.topView.mas_bottom).offset(2 * margin);
        make.height.mas_equalTo(showViewHeight);
    }];
    [self.takeLookDateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.containerView);
        make.top.equalTo(weakSelf.orderTitleView.mas_bottom);
        make.height.mas_equalTo(showViewHeight);
    }];
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.containerView);
        make.top.equalTo(weakSelf.takeLookDateView.mas_bottom);
        make.height.mas_equalTo(showViewHeight);
    }];
    [self.orderHouseTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.containerView);
        make.top.equalTo(weakSelf.phoneView.mas_bottom);
        make.height.mas_equalTo(showViewHeight);
    }];
    [self.regionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.containerView);
        make.top.equalTo(weakSelf.orderHouseTitleView.mas_bottom);
        make.height.mas_equalTo(showViewHeight);
    }];
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.containerView).offset(margin);
        make.top.equalTo(weakSelf.regionView.mas_bottom).offset(margin);
        make.right.equalTo(weakSelf.containerView).offset(-2 * margin);
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.containerView);
        make.top.equalTo(weakSelf.detailLabel.mas_bottom).offset(2 * margin);
        make.height.mas_equalTo(1);
    }];
    [self.rentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.containerView);
        make.top.equalTo(weakSelf.lineView.mas_bottom);
        make.height.mas_equalTo(showViewHeight);
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.rentView.mas_bottom).offset(2 * margin);
        make.right.equalTo(weakSelf.containerView).offset(-2 * margin);
        make.size.mas_equalTo(buttonSize);
        make.bottom.lessThanOrEqualTo(weakSelf.containerView);
    }];
    
    [self.refuseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.confirmButton);
        make.left.equalTo(weakSelf.containerView).offset(2 * margin);
        make.size.mas_equalTo(buttonSize);
    }];
}



@end
